package com.bank.basic.service.impl;

import com.bank.basic.mapper.LoanMapper;
import com.bank.basic.service.FileService;
import com.bank.basic.service.LoanService;
import com.bank.basic.util.ClientFactory;
import com.bank.basic.util.EntityFactory;
import com.bank.basic.util.MapperFactory;
import com.bank.basic.util.VoFactory;
import com.bank.common.commonUtil.FileUtil;
import com.bank.model.entity.*;
import com.bank.model.enums.LoanStatusEnum;
import com.bank.model.enums.LoanTypeEnum;
import com.bank.model.vo.loan.LoanManagementVo;
import com.bank.model.vo.loan.LoanVo;
import com.bank.model.vo.user.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/2/16 17:21
 */


@Service
public class LoanServiceImpl implements LoanService {
    @Autowired
    MapperFactory mapperFactory;
    @Autowired
    EntityFactory entityFactory;
    @Autowired
    VoFactory voFactory;
    @Autowired
    ClientFactory clientFactory;
    @Autowired
    FileService fileService;

    public void generateLoan(int type,String totalMoney){
        Loan loan = new Loan();
        loan.setBorrowId(0L);
        loan.setApproverId(0L);
        loan.setType(type);
        loan.setTotalMoney(totalMoney);
        loan.setPaidMoney("0");
        loan.setInterestRate("0.33");
        mapperFactory.getLoanMapper().insert(loan);
    }

    @Override
    public List<LoanVo> queryListVo(Long userId) {
        List<Loan> loans = queryList(userId);
        List<LoanVo> loanVos = new ArrayList<>();
        for(Loan loan: loans){
            loanVos.add(voFactory.getLoanVo(loan));
        }
        return loanVos;
    }

    @Override
    public List<LoanVo> queryListVoManager() {
        List<UserVo> userVos = clientFactory.getListService().getUserVo();
        List<LoanVo> loanVos = new ArrayList<>();
        for(UserVo userVo:userVos){
            List<Loan> loans = queryList(userVo.getId());
            loanVos.addAll(voFactory.getLoanVos(loans));
        }
        return loanVos;
    }

    @Override
    public List<LoanManagementVo> queryLoanManagementVo() {
        List<UserVo> userVos = clientFactory.getListService().getUserVo();
        List<LoanManagementVo> loanManagementVos = new ArrayList<>();
        for(UserVo userVo:userVos){
            List<Loan> loans = queryList(userVo.getId());
            loanManagementVos.addAll(voFactory.getLoanManagementVos(userVo,loans));
        }
        return loanManagementVos;
    }

    @Override
    public List<Loan> queryAll() {
        List<Loan> loans = mapperFactory.getLoanMapper().selectBatchIds(null);
        return loans;
    }

    @Override
    public List<Loan> queryList(Long userId) {
        QueryWrapper<Loan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("borrow_id",userId);
        List<Loan> loans = mapperFactory.getLoanMapper().selectList(queryWrapper);
        return loans;
    }
    @Override
    public List<Loan> queryAccount(String accountId) {
        QueryWrapper<Loan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("borrow_id",accountId);
        List<Loan> loans = mapperFactory.getLoanMapper().selectList(queryWrapper);
        return loans;
    }
    @Override
    public List<Loan> queryUser(Long userId) {
        List<Account> accounts = clientFactory.getAccountService().getAccounts(userId);
        List<Loan> loans = new ArrayList<>();
        for(Account account:accounts){
            loans.addAll(queryAccount(account.getAccountId()));
        }
        return loans;
    }

    @Override
    public Loan queryLoan(Date date) {
        QueryWrapper<Loan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("created_time",date);
        Loan loan = mapperFactory.getLoanMapper().selectOne(queryWrapper);
        return loan;
    }

    @Override
    public void addLoan(LoanVo loanVo, MultipartFile file) throws IOException {
        Loan loan = entityFactory.getLoan(loanVo);
        if(loanVo.getType().equals(LoanTypeEnum.MortgageLoan.getMessage())){
            if(file == null) return;
        }
        mapperFactory.getLoanMapper().insert(loan);
        fileService.saveFile(file,loan.getId());
    }

    @Override
    public void updateLoanState(Long loanId, Boolean ifPass) {
        Loan loan = mapperFactory.getLoanMapper().selectById(loanId);
        if(loan.getStatus()!=LoanStatusEnum.APPROVING.getCode()) return;
        if(ifPass){
            loan.setStatus(LoanStatusEnum.NOT.getCode());
        }else{
            loan.setStatus(LoanStatusEnum.FAIL.getCode());
        }
        mapperFactory.getLoanMapper().updateById(loan);
    }

    @Override
    public void delLoan(Long loanId) {
        mapperFactory.getLoanMapper().deleteById(loanId);
    }

    @Override
    public void download(Long loanId, HttpServletResponse httpServletResponse) {

    }

}
